package com.visikard.business;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.visikard.net.VKregisterException;
import com.visikard.net.VKregisterNetworkException;
import com.visikard.request.ParseRequesUtils;
import com.visikard.request.TransactionHistoryRequest;
import com.visikard.response.BaseResponse;
import com.visikard.response.TransactionHistoryResponse;
import com.visikard.response.TransactionHistoryRewardPunchKardResponse;

public class TransactionHistoryBUS {

	// get gift kards transaction history by date
	public TransactionHistoryResponse getTransactionHistoryByDateNew(long uid, String isStaff, int txType,
			String sDate, String eDate, String timezone, int pageIndex, int pageSize) {

		TransactionHistoryResponse result = new TransactionHistoryResponse();
		TransactionHistoryRequest ar = new TransactionHistoryRequest();
		try {
			String jsonResult = ar.getTransactionHistoryByDateNew(uid, isStaff, txType, sDate, eDate, timezone,
					pageIndex, pageSize);
			result.extractDataFromServer(jsonResult);
		}
		catch (VKregisterNetworkException ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		catch (Exception ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(ex.getMessage());
		}

		return result;
	}

	// get gift kards transaction history by date
	public TransactionHistoryResponse getTransactionHistoryGiftKardByDate(long uid, String isStaff, String sDate,
			String eDate, int pageIndex, int pageSize) {

		TransactionHistoryResponse result = new TransactionHistoryResponse();
		TransactionHistoryRequest ar = new TransactionHistoryRequest();
		try {
			String jsonResult = ar.getTransactionHistoryGiftKardByDate(uid, isStaff, sDate, eDate, pageIndex, pageSize);
			result.extractDataFromServer(jsonResult);
		}
		catch (VKregisterNetworkException ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		catch (Exception ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(ex.getMessage());
		}

		return result;
	}

	// get coupon kards transaction history by date
	public TransactionHistoryResponse getTransactionHistoryCouponKardByDate(long uid, String isStaff, String sDate,
			String eDate, int pageIndex, int pageSize) {

		TransactionHistoryRequest ar = new TransactionHistoryRequest();
		TransactionHistoryResponse result = new TransactionHistoryResponse();

		try {
			String jsonResult = ar.getTransactionHistoryCouponKardByDate(uid, isStaff, sDate, eDate, pageIndex,
					pageSize);
			result.extractDataFromServer(jsonResult);
		}
		catch (VKregisterNetworkException ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		catch (Exception ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		return result;
	}

	// get punch kards transaction history: New API
	public TransactionHistoryResponse getTransactionHistoryPunchKard(long uid, String isStaff, int pageIndex,
			int pageSize) {

		TransactionHistoryRequest ar = new TransactionHistoryRequest();
		TransactionHistoryResponse result = new TransactionHistoryResponse();

		try {
			String jsonResult = ar.getTransactionHistoryPunchKard(uid, isStaff, pageIndex, pageSize);
			result.extractDataFromServer(jsonResult);
		}
		catch (VKregisterNetworkException ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		catch (Exception ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		return result;
	}

	public TransactionHistoryRewardPunchKardResponse getTransactionHistoryRewardedPunchKard(long uid, int pageIndex,
			int pageSize) {

		TransactionHistoryRequest ar = new TransactionHistoryRequest();
		TransactionHistoryRewardPunchKardResponse result = new TransactionHistoryRewardPunchKardResponse();

		try {
			String jsonResult = ar.getTransactionHistoryRewardedPunchKard(uid, pageIndex, pageSize);

			if (jsonResult != null && ParseRequesUtils.isValidJsonArray(jsonResult)) {
				String jsonListItem = jsonResult;
				// get number gift card
				if (pageIndex == 1) {
					try {
						JSONArray jarrRoot = new JSONArray(jsonResult);
						JSONObject jobjRoot = jarrRoot.getJSONObject(0);
						result.setNbGiftKardRewarded(jobjRoot.getInt("countGiftRewards"));
						result.setNbGiftKardRedeemed(jobjRoot.getInt("countGiftRedeem"));
						jsonListItem = jobjRoot.getJSONArray("punkReward").toString();
					}
					catch (JSONException ex) {
						// handle error here
						Log.e("getListTrasaction error: ", ex.toString());
					}
				}
				if (jsonListItem != null && ParseRequesUtils.isValidJsonArray(jsonListItem)) {
					result.setListTransactionRewardPunchKards(ParseRequesUtils
							.parseTransactionRewardPunchKard(jsonListItem));
				}
			}
		}
		catch (VKregisterNetworkException ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		catch (VKregisterException ex) {
			ex.printStackTrace();
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(ex.getMessage());
		}
		return result;
	}

}
